package com.data.modules.smzh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.common.system.vo.LoginUser;
import com.data.modules.logo.entity.SysLogo;
import com.data.modules.smzh.entity.Building;
import com.data.modules.smzh.entity.Project;
import com.data.modules.smzh.service.IBuildingService;
import com.data.modules.smzh.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: project
 * @Author: data-boot
 * @Date:   2022-11-26
 * @Version: V1.0
 */
@Api(tags="project")
@RestController
@RequestMapping("/smzh/project")
@Slf4j
public class ProjectController extends BootController<Project, IProjectService> {
	@Autowired
	private IProjectService projectService;

	@Autowired
	private IBuildingService buildService;


	/**
	 * 分页列表查询
	 *
	 * @param project
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "project-分页列表查询")
	@ApiOperation(value="project-分页列表查询", notes="project-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Project project,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		project.setAppId(Integer.valueOf(sysUser.getId()));

		QueryWrapper<Project> queryWrapper = QueryGenerator.initQueryWrapper(project, req.getParameterMap());
		Page<Project> page = new Page<Project>(pageNo, pageSize);
		IPage<Project> pageList = projectService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 @ApiOperation(value="获取当前登录人所有的项目信息", notes="获取当前登录人所有的项目信息")
	 @GetMapping(value = "/getProjectList")
	 public Result<?> queryProjectList(Project project) {

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 project.setAppId(Integer.valueOf(sysUser.getId()));

		 LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<Project>();
		 wrapper.eq(Project::getAppId,Integer.valueOf(sysUser.getId()));
		 List<Project> list = projectService.list(wrapper);
		 return Result.OK(list);
	 }

	@ApiOperation(value="获取当前登录人所有的建筑编号信息", notes="获取当前登录人所有的建筑编号信息")
	@GetMapping(value = "/getBuild")
	public Result<?> queryBuildList(Building project) {

	/*	LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		project.setAppId(Integer.valueOf(sysUser.getId()));
*/
		LambdaQueryWrapper<Building> wrapper = new LambdaQueryWrapper<Building>();
		wrapper.select(Building::getBuildNo);
		wrapper.eq(Building::getProjectNo,project.getProjectNo());
		//wrapper.eq(Building::getAppId,Integer.valueOf(sysUser.getId()));
		wrapper.groupBy(Building::getBuildNo);
		List<Building> list = buildService.list(wrapper);
		return Result.OK(list);
	}


	@ApiOperation(value="获取当前登录人所有的建筑栋信息", notes="获取当前登录人所有的建筑栋信息")
	@GetMapping(value = "/getUnit")
	public Result<?> queryBuildUnitList(Building project) {

		/*LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		project.setAppId(Integer.valueOf(sysUser.getId()));
*/
		LambdaQueryWrapper<Building> wrapper = new LambdaQueryWrapper<Building>();
		wrapper.select(Building::getUnitNo);
		//wrapper.eq(Building::getAppId,Integer.valueOf(sysUser.getId()));
		wrapper.eq(Building::getBuildNo,project.getBuildNo());
		wrapper.groupBy(Building::getUnitNo);
		List<Building> list = buildService.queryBuildUnitList(wrapper);
		return Result.OK(list);
	}
	/**
	 *   添加
	 *
	 * @param project
	 * @return
	 */
	@AutoLog(value = "project-添加")
	@ApiOperation(value="project-添加", notes="project-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Project project) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		project.setAppId(Integer.valueOf(sysUser.getId()));

		projectService.save(project);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param project
	 * @return
	 */
	@AutoLog(value = "project-编辑")
	@ApiOperation(value="project-编辑", notes="project-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Project project) {
		projectService.updateById(project);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "project-通过id删除")
	@ApiOperation(value="project-通过id删除", notes="project-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		projectService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "project-批量删除")
	@ApiOperation(value="project-批量删除", notes="project-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.projectService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "project-通过id查询")
	@ApiOperation(value="project-通过id查询", notes="project-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Project project = projectService.getById(id);
		if(project==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(project);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param project
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Project project) {
        return super.exportXls(request, project, Project.class, "project");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Project.class);
    }

}
